﻿// /*
//  * @Author: Dreamy Icecream 
//  * @Date: 2020-12-31 13:57:05 
//  * @Description: 高斯模糊
//  * @Last Modified by: Dreamy Icecream 
//  * @Last Modified time: 2020-12-31 13:57:05 
//  */

Shader "Custom/GaussianBlur" {
    
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _BlurSize ("模糊大小", Float) = 1.0
    }

    SubShader {
        
        CGINCLUDE

        #include "UnityCG.cginc"

        sampler2D _MainTex;
        half4 _MainTex_TexelSize;
        float _BlurSize;

        struct v2f{
            float4 pos : SV_POSITION;
            half2 uv[5] : TEXCOORD0;
        };

        /** 纵向 */
        v2f vertBlurVertical(appdata_img v) {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);

            half2 uv = v.texcoord;

            o.uv[0] = uv;
            o.uv[1] = uv + float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
            o.uv[2] = uv - float2(0.0, _MainTex_TexelSize.y * 1.0) * _BlurSize;
            o.uv[3] = uv + float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSize;
            o.uv[4] = uv - float2(0.0, _MainTex_TexelSize.y * 2.0) * _BlurSize;

            return o;
        }

        /** 横向 */
        v2f vertBlurHorizontal(appdata_img v) {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);

            half2 uv = v.texcoord;

            o.uv[0] = uv;
            o.uv[1] = uv + float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
            o.uv[2] = uv - float2(_MainTex_TexelSize.x * 1.0, 0.0) * _BlurSize;
            o.uv[3] = uv + float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;
            o.uv[4] = uv - float2(_MainTex_TexelSize.x * 2.0, 0.0) * _BlurSize;

            return o;
        }

        fixed4 fragBlur(v2f i) : SV_TARGET{

            //高斯权重
            float weight[3] = {0.4026, 0.2442, 0.0545};
            
            fixed3 sum = tex2D(_MainTex, i.uv[0]).rgb * weight[0];
            for(int index = 1; index < 3; index++){
                sum += tex2D(_MainTex, i.uv[index * 2 - 1]).rgb * weight[index];
                sum += tex2D(_MainTex, i.uv[index * 2]).rgb * weight[index];
            }
            return fixed4(sum, 1.0);
        }

        ENDCG

        ZTest Always Cull Off ZWrite Off

        Pass {
            NAME "GAUSSIAN_BLUR_VERTICAL"

            CGPROGRAM

            #pragma vertex vertBlurVertical
            #pragma fragment fragBlur

            ENDCG
        }

        Pass {
            NAME "GAUSSIAN_BLUR_HORIZONTAL"

            CGPROGRAM

            #pragma vertex vertBlurHorizontal
            #pragma fragment fragBlur

            ENDCG
        }

    }

    FallBack Off
}
